import {
  reqGetCategory1List,
  reqGetCategory2List,
  reqGetCategory3List
} from '@/api/product/category'
const state = {
  category1Id: '',
  category2Id: '',
  category3Id: '',
  category1List: [],
  category2List: [],
  category3List: []
}

const actions = {
  // 获取一级菜单
  async getCategory1List({ commit }) {
    const { data } = await reqGetCategory1List()
    commit('GET_CATEGORY1_LIST', data)
  },
  // 获取二级菜单和一级菜单的id
  async getCategory2List({ commit }, category1Id) {
    const { data } = await reqGetCategory2List(category1Id)
    commit('GET_CATEGORY2_LIST', { category2List: data, category1Id })
  },
  // 获取三级菜单和二级菜单的id
  async getCategory3List({ commit }, category2Id) {
    const { data } = await reqGetCategory3List(category2Id)
    commit('GET_CATEGORY3_LIST', { category3List: data, category2Id })
  }
}

const mutations = {
  // 一级菜单存储
  GET_CATEGORY1_LIST(state, data) {
    state.category1List = data
  },
  // 二级菜单/一级id存储
  GET_CATEGORY2_LIST(state, { category2List, category1Id }) {
    state.category2List = category2List
    state.category1Id = category1Id
    // 一级菜单改变,二级显示清空,三级菜单清空
    state.category2Id = ''
    state.category3Id = ''
    state.category3List = []
  },
  // 三级菜单/二级id存储
  GET_CATEGORY3_LIST(state, { category3List, category2Id }) {
    state.category3List = category3List
    state.category2Id = category2Id
    // 二级菜单改变,三级显示清空
    state.category3Id = ''
  },
  // 三级id存储
  SET_CATEGORY3_ID(state, category3Id) {
    state.category3Id = category3Id
  },
  // 清空Attr中的所有内容
  CLEAR_ALL_ATTR(state) {
    state.category1Id = ''
    state.category2Id = ''
    state.category3Id = ''
    state.category1List = []
    state.category2List = []
    state.category3List = []
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
